package yuan.serializer;

import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
import org.apache.kafka.common.serialization.Serializer;
import yuan.Company;

import java.util.Map;

public class CompanySerializer implements Serializer<Company> {
    @Override
    public void configure(Map<String, ?> map, boolean b) {

    }

    @Override
    public byte[] serialize(String s, Company company) {

        if (company == null) {
            return null;
        }

         Schema schema = (Schema) RuntimeSchema.getSchema(company.getClass());
        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
        byte[] protostuff = null;

        try  {
            protostuff = ProtostuffIOUtil.toByteArray(company , schema, buffer) ;
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }finally{
            buffer.clear();
        }

        return protostuff;


    }

    @Override
    public void close() {

    }
}
